package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.VideoCardDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.MessageNotice;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class MessageCommentFragment extends BaseListFragment<MessageCommentFragment.CommentViewHolder, MessageNotice> {

    public static MessageCommentFragment create() {
        MessageCommentFragment fragment = new MessageCommentFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    public MessageCommentFragment() {
    }

    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<MessageNotice>>(new TypeReference<Result<List<MessageNotice>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }

            @Override
            public void responseInfo(Result<List<MessageNotice>> data) {
                addResult(data);
            }
        });
    }


    /**
     * 跳转到视频detail
     *
     * @param cardid
     */
    public void forwardVideoDetail(Long cardid) {
        Intent intent = new Intent(this.getActivity(), VideoCardDetailActivity.class);
        intent.putExtra("cardid", cardid);
        startActivity(intent);
    }


    public interface CommentItemListener {
        public void commentOnClick(MessageNotice notice);
    }


    @Override
    public LinearLayoutManager getLayoutManager() {
        return null;
    }

    @Override
    protected CommentViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_message_comment, parent, false);
        CommentViewHolder viewHolder = new CommentViewHolder(view, new CommentItemListener() {
            @Override
            public void commentOnClick(MessageNotice notice) {
                forwardVideoDetail(notice.videoCard.cardid);
            }
        });
        return viewHolder;
    }

    @Override
    protected CommentViewHolder createfootViewHolder(View view) {

        return new CommentViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<MessageNotice> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_notice_list,MessageNotice.TYPE_NOTICE_COMMENT, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }


    public static class CommentViewHolder extends BaseRecycleViewHolder<MessageNotice> {
        private RoundNetworkImageView mavatar;
        private TextView mNickName;
        private TextView mPubTime;
        private View itemView;
        private NetworkImageView mVideoThum;
        private TextView mNoticeDesc;
        private CommentItemListener listener;
        private View itemVIew;

        public CommentViewHolder(View itemView, CommentItemListener listener) {
            this(itemView);
            this.listener = listener;
        }

        public CommentViewHolder(View itemView) {
            super(itemView);
            mVideoThum = (NetworkImageView) itemView.findViewById(R.id.video_thum);
            mNoticeDesc = (TextView) itemView.findViewById(R.id.tv_notice_desc);
            mavatar = (RoundNetworkImageView) itemView.findViewById(R.id.iv_user_avatar);
            mNickName = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            mPubTime = (TextView) itemView.findViewById(R.id.tv_pubtime);
            this.itemView = itemView;
        }

        @Override
        public void renderView(final MessageNotice data, int position) {
            if (data.videoCard != null) {
                mVideoThum.setImageUrl(data.videoCard.video.thumUrl, HttpUtils.getImageLoader());
            } else {
                mVideoThum.setImageUrl(null, HttpUtils.getImageLoader());
            }
            mNoticeDesc.setText(data.content);
            mavatar.setImageUrl(data.from_user.avatar, HttpUtils.getAvatarImageLoader());
            mNickName.setText(data.from_user.nickname);
            if (data.ctime == null) data.ctime = System.currentTimeMillis();
            mPubTime.setText(DateUtil.timeStamp2Date(data.ctime, null));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.commentOnClick(data);
                }
            });

        }
    }

}
